# coding=utf-8
import random
import time
from typing import Dict, Optional

from obespoir.server.frame.frame_server import FrameServer

from package.game_const import GameStatus, GameType, ReadyStatus


class BaseRoom(object):

    def __init__(self, game_id: str, game_type: int, room_type: int) -> None:
        self.game_id: str = game_id
        self.game_type: int = game_type
        self.room_type: int = room_type
        self.game_status: int = GameStatus.NOT_START
        # user_id: ReadyStatus
        self.members: Dict[str, int] = {}

        self.frame_server: Optional[FrameServer] = None

        self.random_seed: int = random.randint(0, 1000000)
        self.create_time: int = int(time.time())

    @property
    def cur_member_num(self) -> int:
        return len(self.members)

    def is_full(self) -> bool:
        if self.game_type == GameType.SINGLE:
            return len(self.members) == 1
        elif self.game_type == GameType.DOUBLE:
            return len(self.members) == 2
        return False

    def is_all_ready(self) -> bool:
        for status in self.members.values():
            if status != ReadyStatus.READY:
                return False
        return True

    def is_all_over(self) -> bool:
        for status in self.members.values():
            if status != ReadyStatus.GAME_OVER:
                return False
        return True

    def is_time_out(self) -> bool:
        return int(time.time()) - self.create_time > 60 * 10
